let url =
  "http://www.domain.com/?user=anonymous&id=123&id=456&city=%E5%8C%97%E4%BA%AC&enabled";
let res = parseParam(url);
console.log(res);
/* 结果
{ user: 'anonymous',
  id: [ 123, 456 ], // 重复出现的 key 要组装成数组，能被转成数字的就转成数字类型
  city: '北京', // 中文需解码
  enabled: true, // 未指定值得 key 约定为 true
}
*/

function parseParam(url) {
  const paramStr = /.+\?(.+)$/.exec(url)[1];
  const paramArr = paramStr.split("&");

  let paramsObj = {};
  paramArr.forEach((item) => {
    if (/=/.test(item)) {
      let [key, val] = item.split("=");
      val = decodeURIComponent(val);
      val = /\d+/.test(val) ? parseFloat(val) : val;
      paramsObj.hasOwnProperty(key)
        ? (paramsObj[key] = [].concat(paramsObj[key], val))
        : (paramsObj[key] = val);
    } else {
      let key = item.split("=")[0];
      paramsObj[key] = true;
    }
  });

  return paramsObj;
}
